<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
    	<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
		
		<link rel="stylesheet" href="libs/bootstrap/css/bootstrap.min.css"/>
		
		<script src="libs/jquery.min.js"></script>
		<script src="libs/bootstrap/js/bootstrap.min.js"></script>
		
		<title>注册</title>
		
	</head>
	<body>
		
		<nav class="navbar navbar-inverse" role="navigation">
		  	<div class="container">
		    <!-- Brand and toggle get grouped for better mobile display -->
			    <div class="navbar-header">
			      	<a class="navbar-brand" href="#">智游教育</a>
			    </div>
			
			    <!-- Collect the nav links, forms, and other content for toggling -->
			    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
			      	<ul class="nav navbar-nav navbar-right">
				        <li><a href="#" onclick="questionBtnClick(event)"><span class="glyphicon glyphicon-question-sign"></span>提问</a></li>
				        <li class="dropdown">
				          	<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span>用户名<span class="caret"></span></a>
				          	<ul class="dropdown-menu" role="menu">
					            <li><a href="#">上传头像</a></li>
					            <li><a href="#">退出登录</a></li>
				          	</ul>
				        </li>
			      	</ul>
			    </div><!-- /.navbar-collapse -->
		  	</div><!-- /.container-fluid -->
		</nav>
		
		
		<div class="container">
			
			<form role="form" id="regist-form">
				
				<div class="form-group">
					<label for="">账号</label>
					<input class="form-control" type="text" name="account" autocomplete="off" required placeholder="数字、字母、下划线，1到6位"/>
				</div>
				
				<div class="form-group">
					<label for="">密码</label>
					<input class="form-control" type="password" name="psw" required/>
				</div>
				
				<div class="form-group">
					<label for="">确认密码</label>
					<input class="form-control" type="password" id="confirm" required/>
				</div>
				
				<button type="submit" class="btn btn-success form-control "/>注册</button>
				
			</form>
			
		</div>
		
		<script>
			
			$("#regist-form").submit(function(e){
				e.preventDefault();
				if(!$("[name=account]").val().match(/^\w{1,6}$/)){
					alert("账号格式不合符要求");
					return;
				}
				if($("[name=psw]").val()!=$("#confirm").val()){
					alert("两次密码输入不一致");
					return;
				}
				
				var data = $("#regist-form").serialize();
				
				$.post("/regist-api",data,function(resData){
					alert(resData.msg);
					
					if(resData.err == 0){
						//注册成功之后，跳转登录页面
						location.href = "login.html";
					}
				});
			});
			
			function questionBtnClick(e){
	            e.preventDefault();
	
	            if($.cookie("token")){
		        //已经登录，跳转提问页面。
		        location.href = "/ask.html";
	            }else{
		        //未登录，跳转登录页面
		        location.href = "login.html";
	            }
	
            }
			
		</script>
		
	</body>
	<script src="libs/jquery.cookie.js"></script>
</html>
